package newflink3;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.hadoop.conf.Configuration;

public class job3 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);
        env.enableCheckpointing(5000);

        KafkaSource<String> source = KafkaSource.<String>builder()
                .setBootstrapServers("bigdata1:9092")
                .setGroupId("kafka3job3")
                .setTopics("dim_customer_login_log")
                .setStartingOffsets(OffsetsInitializer.earliest())
                .setValueOnlyDeserializer(new SimpleStringSchema())
                .build();

        WatermarkStrategy<String> watermarkStrategy = WatermarkStrategy.<String>noWatermarks();

        SingleOutputStreamOperator<String[]> kafkajob3 = env.fromSource(source, watermarkStrategy, "kafkajob3")
                .map(s -> {
                    int i = s.lastIndexOf("}");

                    return s.substring(20, i);
                }).map(s -> s.replace(",", "").split("\\|"));

        Configuration entries = new Configuration();
        entries.set("hbase.zookeeper.quorum" , "bigdata1,bigdata2,bigdata3");

        kafkajob3.addSink(new HbaseSink(entries));

        env.execute();
    }
}
